package br.com.foxinline.servico;

import br.com.foxinline.modelo.TipoSaida;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author cadomeireles
 */
@Stateless
public class TipoSaidaServico extends ServicoGenerico<TipoSaida> {

    public TipoSaidaServico() {
        super(TipoSaida.class);
    }

    @Override
    public void deletar(TipoSaida tipoSaida) {
        tipoSaida.setAtivo(false);
        super.atualizar(tipoSaida);
    }

    public List<TipoSaida> pesquisar(TipoSaida tipoSaida) {
        String sql = "select tipoSaida from TipoSaida tipoSaida where tipoSaida.ativo = true";
        
        if (!tipoSaida.getDescricao().isEmpty()) {
            sql += " and lower(tipoSaida.descricao) like '%" + tipoSaida.getDescricao().toLowerCase() + "%'";
        }
        
        Query query = getEntityManager().createQuery(sql);
        return query.getResultList();
    }

    public List<TipoSaida> autocompletar(String query){
        return buscarPeloNome(query);
    }

    private List<TipoSaida> buscarPeloNome(String query){
        List<TipoSaida> saidas = getEntityManager().createQuery("select tipoSaida from " + TipoSaida.class.getSimpleName() + " tipoSaida where tipoSaida.ativo = true and lower(tipoSaida.descricao) like '%" + query.toLowerCase().trim() + "%'").getResultList();
        return saidas;
    }      
    
}
